package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.bean.RateInfo
import com.changpay.zywy.ui.dialog.EditRateDialog
import kotlinx.android.synthetic.main.activity_set_lower_price.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class SetLowerPriceActivity : BaseActivity(), IView {


    private var type: String = ""
    private var member_id = ""
    private var produce_id = ""


    override fun attachLayoutRes(): Int {
        return R.layout.activity_set_lower_price
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)

    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_name -> {
                if (produce_id.isEmpty()) {
                    showToast("请选择修改产品名称")
                    return@OnClickListener
                }
                Intent(this, SelectMemberActivity::class.java).run {
                    putExtra("type", 1)
                    putExtra("produce_id", produce_id)
                    startActivityForResult(this, 100)
                }
            }
            R.id.tv_select_produce -> {
                Intent(this, SelectProduceActivity::class.java).run {
                    putExtra("member_id", member_id)
                    putExtra("gc_id",  intent.getStringExtra("gc_id"))
                    startActivityForResult(this, 100)
                }
            }
            R.id.iv_cloud_pay -> showEditDialog(1)
            R.id.iv_credit_card -> showEditDialog(2)
            R.id.iv_bank_card -> showEditDialog(3)
            R.id.iv_fast_pay -> showEditDialog(4)
            R.id.iv_code_pay -> showEditDialog(5)
            R.id.iv_award_money -> showEditIntDialog()
            R.id.tv_apply -> updateRate()


//                R.id. ->
//                R.id. -> startActivity(ForgetPasswordActivity::class.java)
        }
    }

    override fun initView() {
        tv_title.text = "设置下级政策"
        iv_back.setOnClickListener(onClickListener)
        tv_name.setOnClickListener(onClickListener)
        tv_select_produce.setOnClickListener(onClickListener)
        iv_cloud_pay.setOnClickListener(onClickListener)
        iv_credit_card.setOnClickListener(onClickListener)
        iv_bank_card.setOnClickListener(onClickListener)
        iv_fast_pay.setOnClickListener(onClickListener)
        iv_code_pay.setOnClickListener(onClickListener)
        iv_award_money.setOnClickListener(onClickListener)
        rg_mine_lower.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_mine -> {
                    lineFull_refund_open = 0
                    tv_apply.isEnabled = true
                }
                R.id.rb_lower -> {
                    lineFull_refund_open = 1
                    tv_apply.isEnabled = true
                }
            }
        }
        tv_apply.setOnClickListener(onClickListener)

        // 获取传进来的数据
        type = intent.getStringExtra("type")
        if (type == "qudao")  // 渠道进来的话
        {
            member_id = intent.getStringExtra("member_id")
            produce_id = intent.getStringExtra("goods_id")
            tv_name.text = intent.getStringExtra("truename")
            tv_select_produce.text = intent.getStringExtra("gc_name") + " " +
                    intent.getStringExtra("goods_name") + " " + intent.getStringExtra("goods_serial")
            // 隐藏控件
            iv_right_one.visibility = View.INVISIBLE
            iv_right_two.visibility = View.INVISIBLE
            tv_name.isEnabled = false
            tv_select_produce.isEnabled = false
            // 加载数据
            loadProduceData()
        }
    }

    override fun start() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            200 -> {
                member_id = data?.getStringExtra("member_id").toString()
                tv_name.text = data?.getStringExtra("member_name").toString()
                loadProduceData()
            }
            300 -> {
                member_id = ""
                tv_name.text = ""
                setData(null)
                produce_id = data?.getStringExtra("produce_id").toString()
                tv_select_produce.text = data?.getStringExtra("produce_name").toString()

            }
        }
    }

    private fun loadProduceData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["goods_id"] = produce_id
        param["others_member_id"] = member_id
        param["is_set_mb_rate"] = "1"
        param["client"] = "android"
        RetrofitHelper.service.showRateInfo(param).sss(this)
        {
            setData(it.result)
            tv_apply.isEnabled = true
        }
    }

    private var canChange: Boolean = false  // 用于控制是否可以被编辑
    private var canShow: Boolean = false  // 用于是否展满返开关

    private lateinit var activate_rewards: String

    private var lineFull_refund_open = 0 // 达标返现开关：0 自己、1 下级

    private var show_refund_open = 0 // 	否显示开关（满返返现的）：1 显示、0 不显示

    private lateinit var cloud_merchant_rate: String

    private lateinit var bankCard_merchant_rate: String

    private lateinit var lineCard_merchant_rate: String

    private lateinit var quickPay_merchant_rate: String

    private lateinit var scaveCode_merchant_rate: String

    private fun setData(dateBean: RateInfo?) {
        canChange = if (dateBean != null) {
            tv_cloud_pay.text = dateBean.cloud_admin_share + "%"
            tv_credit_card.text = dateBean.lineCard_admin_share + "%"
            tv_bank_card.text = dateBean.bankCard_admin_share + "%"
            tv_fast_pay.text = dateBean.quickPay_admin_share + "%"
            tv_code_pay.text = dateBean.scaveCode_admin_share + "%"
            tv_award_money.text = dateBean.activate_rewards.toString()
            lineFull_refund_open = dateBean.lineFull_refund_open
            show_refund_open = dateBean.show_refund_open

            when (lineFull_refund_open) {
                0 -> rg_mine_lower.check(R.id.rb_mine)
                1 -> rg_mine_lower.check(R.id.rb_lower)
            }
            canShow = show_refund_open == 1
            true
        } else {
            tv_cloud_pay.text = "0.00%"
            tv_credit_card.text = "0.00%"
            tv_bank_card.text = "0.00%"
            tv_fast_pay.text = "0.00%"
            tv_code_pay.text = "0.00%"
            tv_award_money.text = "0"
            lineFull_refund_open = 0
            show_refund_open = 0
            canShow = false
            false
        }
        tv_cloud_pay_change.text = ""
        tv_credit_card_change.text = ""
        tv_bank_card_change.text = ""
        tv_fast_pay_change.text = ""
        tv_code_pay_change.text = ""
        tv_award_money_change.text = ""
        cloud_merchant_rate = ""
        bankCard_merchant_rate = ""
        lineCard_merchant_rate = ""
        quickPay_merchant_rate = ""
        scaveCode_merchant_rate = ""
        activate_rewards = ""
        rl_award.visibility = if (canShow) View.VISIBLE else View.GONE
        tv_apply.isEnabled = false
    }

    private fun showEditDialog(type: Int) {
        if (canChange) {
            var title = ""
            var dialogClickListener: EditRateDialog.OnDialogClickListener? = null
            when (type) {
                1 -> {
                    title = "云闪付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { _, inputText ->
                        tv_cloud_pay_change.text = "修改为  $inputText%"
                        cloud_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                2 -> {
                    title = "信用卡"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_credit_card_change.text = "修改为  $inputText%"
                        lineCard_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                3 -> {
                    title = "银行卡"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_bank_card_change.text = "修改为  $inputText%"
                        bankCard_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                4 -> {
                    title = "快捷支付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_fast_pay_change.text = "修改为  $inputText%"
                        quickPay_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                5 -> {
                    title = "扫码支付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_code_pay_change.text = "修改为  $inputText%"
                        scaveCode_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
            }
            val builder = EditRateDialog.Builder(this)
            builder.setInputText("输入的值保留小数点后三位").setTitleText(title).setOnDialogClickListener(dialogClickListener)
            val build = builder.build<EditRateDialog>()
            build.show(supportFragmentManager)
        }
    }

    private fun showEditIntDialog() {
        if (canChange) {
            val title = "激活奖励金额"
            val dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                tv_award_money_change.text = ("修改为  " + inputText + "元")
                activate_rewards = inputText
                tv_apply.setEnabled(true)
            }
            val builder = EditRateDialog.Builder(this)
            builder.setInputText("输入值为整数").setTitleText(title).setOnDialogClickListener(dialogClickListener)
            val build = builder.build<EditRateDialog>()
            build.show(supportFragmentManager)
        }
    }


    val  update = object : IView {
        override fun showLoading() {

        }

        override fun hideLoading() {
        }

        override fun showDefaultMsg(msg: String) {
            showToast(msg)
            finish()
        }

        override fun showMsg(msg: String) {
            showToast(msg)
        }

        override fun showError(errorMsg: String) {
            showToast(errorMsg)
        }
    }
    // 用于特定处理请求
    private fun updateRate() {
        val params = HashMap<String, String>()
        params["key"] = token
        params["client"] = "android"
        params["member_id"] = member_id
        params["goods_id"] = produce_id
        params["cloud_admin_share"] = cloud_merchant_rate
        params["bankCard_admin_share"] = bankCard_merchant_rate
        params["lineCard_admin_share"] = lineCard_merchant_rate
        params["quickPay_admin_share"] = quickPay_merchant_rate
        params["scaveCode_admin_share"] = scaveCode_merchant_rate
        params["activate_rewards"] = activate_rewards
        params["lineFull_refund_open"] = lineFull_refund_open.toString()

            RetrofitHelper.service.set_mb_rate(params).sss(update)
        {
            showToast(it.result.toString())
            this.setResult(200)
            finish()
        }
    }


}
